package org.hiucung.hlsdownloader.ui.screens

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import kotlinx.coroutines.launch
import org.hiucung.hlsdownloader.ui.components.DownloadList
import org.hiucung.hlsdownloader.ui.components.EmptyState
import org.hiucung.hlsdownloader.ui.components.M3U8TaskItemDialog
import org.hiucung.hlsdownloader.viewmodel.M3U8ViewModel

/**
 * @title: MainScreen
 * @projectName HLS Downloader
 * @description: 首页
 * @author Kicc
 * @date 2025/9/4 0:15
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainScreen() {
    var showDialog by remember { mutableStateOf(false) }
    val snackbarHostState = remember { SnackbarHostState() }
    val coroutineScope = rememberCoroutineScope()

    val m3uViewModel: M3U8ViewModel = viewModel()

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("HLS下载工具") },
                actions = {
                    IconButton(onClick = { showDialog = true }) {
                        Icon(Icons.Filled.Add, contentDescription = "添加下载")
                    }
                }
            )
        },
        snackbarHost = { SnackbarHost(snackbarHostState) }
    ) { innerPadding ->
        Box(modifier = Modifier.padding(innerPadding)) {
            val downloadItems by m3uViewModel.downloadItems.collectAsState(initial = emptyList())
            if (downloadItems.isEmpty()) {
                EmptyState()
            } else {
                DownloadList(downloadItems.toList())
            }

            // 添加链接对话框
            if (showDialog) {
                M3U8TaskItemDialog(
                    onDismiss = { showDialog = false },
                    onConfirm = { url, taskName ->
                        if (url.isNotBlank()) {
                            if (taskName.isEmpty()) {
                                m3uViewModel.addDownloadItem(url)
                            } else {
                                m3uViewModel.addDownloadItem(url, taskName)
                            }
                            coroutineScope.launch {
                                snackbarHostState.showSnackbar("已添加下载任务")
                            }
                        }
                        showDialog = false
                    }
                )
            }
        }
    }
}